home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / ImageMap / ImageMap.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-10-12  |  6.9 KB  |  284 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Event;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.Rectangle;
  7. import java.awt.image.CropImageFilter;
  8. import java.awt.image.FilteredImageSource;
  9. import java.awt.image.ImageFilter;
  10. import java.util.Vector;
  11.  
  12. public class ImageMap extends Applet {
  13.    Image baseImage;
  14.    ImageMapArea[] areas;
  15.    static final int BRIGHTER = 0;
  16.    static final int DARKER = 1;
  17.    int hlmode;
  18.    int hlpercent = 50;
  19.    private boolean fullrepaint = false;
  20.    private Rectangle repaintrect = new Rectangle();
  21.    private long lastupdate;
  22.    private static final long UPDATERATE = 100L;
  23.    int pressX;
  24.    int pressY;
  25.  
  26.    Image getHighlight(int x, int y, int w, int h) {
  27.       return this.getHighlight(x, y, w, h, this.hlmode, this.hlpercent);
  28.    }
  29.  
  30.    Image getHighlight(int x, int y, int w, int h, int mode, int percent) {
  31.       return this.getHighlight(x, y, w, h, new HighlightFilter(mode == 0, percent));
  32.    }
  33.  
  34.    Image getHighlight(int x, int y, int w, int h, ImageFilter filter) {
  35.       Image cropped = this.makeImage(this.baseImage, new CropImageFilter(x, y, w, h));
  36.       return this.makeImage(cropped, filter);
  37.    }
  38.  
  39.    Image makeImage(Image orig, ImageFilter filter) {
  40.       return ((Component)this).createImage(new FilteredImageSource(orig.getSource(), filter));
  41.    }
  42.  
  43.    void parseHighlight(String s) {
  44.       if (s != null) {
  45.          if (s.startsWith("brighter")) {
  46.             this.hlmode = 0;
  47.             if (s.length() > "brighter".length()) {
  48.                this.hlpercent = Integer.parseInt(s.substring("brighter".length()));
  49.                return;
  50.             }
  51.          } else if (s.startsWith("darker")) {
  52.             this.hlmode = 1;
  53.             if (s.length() > "darker".length()) {
  54.                this.hlpercent = Integer.parseInt(s.substring("darker".length()));
  55.             }
  56.          }
  57.  
  58.       }
  59.    }
  60.  
  61.    public void init() {
  62.       this.parseHighlight(((Applet)this).getParameter("highlight"));
  63.       this.baseImage = ((Applet)this).getImage(((Applet)this).getDocumentBase(), ((Applet)this).getParameter("img"));
  64.       Vector areaVec = new Vector();
  65.       int var3 = 1;
  66.  
  67.       while(true) {
  68.          String s = ((Applet)this).getParameter("area" + var3);
  69.          ImageMapArea newArea;
  70.          if (s == null) {
  71.             s = ((Applet)this).getParameter("rect" + var3);
  72.             if (s == null) {
  73.                break;
  74.             }
  75.  
  76.             String url = ((Applet)this).getParameter("href" + var3);
  77.             if (url != null) {
  78.                s = s + "," + url;
  79.             }
  80.  
  81.             newArea = new HrefArea();
  82.          } else {
  83.             int classend = s.indexOf(",");
  84.  
  85.             try {
  86.                String name = s.substring(0, classend);
  87.                newArea = (ImageMapArea)Class.forName(name).newInstance();
  88.             } catch (Exception e) {
  89.                ((Throwable)e).printStackTrace();
  90.                break;
  91.             }
  92.  
  93.             s = s.substring(classend + 1);
  94.          }
  95.  
  96.          newArea.init(this, s);
  97.          areaVec.addElement(newArea);
  98.          ++var3;
  99.       }
  100.  
  101.       this.areas = new ImageMapArea[areaVec.size()];
  102.       areaVec.copyInto(this.areas);
  103.       this.checkSize();
  104.    }
  105.  
  106.    synchronized void checkSize() {
  107.       int w = this.baseImage.getWidth(this);
  108.       int h = this.baseImage.getHeight(this);
  109.       if (w > 0 && h > 0) {
  110.          ((Component)this).resize(w, h);
  111.          this.repaintrect.x = this.repaintrect.y = 0;
  112.          this.repaintrect.width = w;
  113.          this.repaintrect.height = h;
  114.          this.fullrepaint = true;
  115.          ((Component)this).repaint();
  116.       }
  117.  
  118.    }
  119.  
  120.    public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
  121.       if ((infoflags & 3) != 0) {
  122.          this.checkSize();
  123.       }
  124.  
  125.       if ((infoflags & 56) != 0) {
  126.          ((Component)this).repaint((infoflags & 48) != 0 ? 0L : 100L, x, y, width, height);
  127.       }
  128.  
  129.       return (infoflags & 96) == 0;
  130.    }
  131.  
  132.    public void paint(Graphics g) {
  133.       synchronized(this){}
  134.  
  135.       try {
  136.          if (this.fullrepaint) {
  137.             g = g.create();
  138.             g.clipRect(this.repaintrect.x, this.repaintrect.y, this.repaintrect.width, this.repaintrect.height);
  139.             this.fullrepaint = false;
  140.          }
  141.       } catch (Throwable var4) {
  142.          throw var4;
  143.       }
  144.  
  145.       if (this.baseImage != null) {
  146.          g.drawImage(this.baseImage, 0, 0, this);
  147.          if (this.areas != null) {
  148.             int i = this.areas.length;
  149.  
  150.             while(true) {
  151.                --i;
  152.                if (i < 0) {
  153.                   break;
  154.                }
  155.  
  156.                if (this.areas[i].active || this.areas[i].entered) {
  157.                   this.areas[i].setState(g, this.areas[i].entered);
  158.                }
  159.             }
  160.          }
  161.  
  162.       }
  163.    }
  164.  
  165.    public void update(Graphics g) {
  166.       if (this.fullrepaint) {
  167.          this.paint(g);
  168.       } else if (this.baseImage != null) {
  169.          g.drawImage(this.baseImage, 0, 0, this);
  170.          if (this.areas != null) {
  171.             int i = this.areas.length;
  172.  
  173.             while(true) {
  174.                --i;
  175.                if (i < 0) {
  176.                   int i = this.areas.length;
  177.  
  178.                   while(true) {
  179.                      --i;
  180.                      if (i < 0) {
  181.                         return;
  182.                      }
  183.  
  184.                      if (this.areas[i].entered) {
  185.                         this.areas[i].setState(g, true);
  186.                      }
  187.                   }
  188.                }
  189.  
  190.                if (this.areas[i].active && !this.areas[i].entered) {
  191.                   this.areas[i].setState(g, false);
  192.                }
  193.             }
  194.          }
  195.       }
  196.    }
  197.  
  198.    public void mouseExit() {
  199.       boolean changed = false;
  200.  
  201.       for(int i = 0; i < this.areas.length; ++i) {
  202.          if (this.areas[i].active) {
  203.             this.areas[i].entered = false;
  204.             changed = true;
  205.          }
  206.       }
  207.  
  208.       if (changed) {
  209.          ((Component)this).repaint();
  210.       }
  211.  
  212.    }
  213.  
  214.    public boolean mouseMove(Event evt, int x, int y) {
  215.       boolean changed = false;
  216.       boolean propagate = true;
  217.  
  218.       for(int i = 0; i < this.areas.length; ++i) {
  219.          if (this.areas[i].inside(x, y)) {
  220.             this.areas[i].entered = propagate;
  221.             if (this.areas[i].terminal) {
  222.                propagate = false;
  223.             }
  224.          } else {
  225.             this.areas[i].entered = false;
  226.          }
  227.  
  228.          if (this.areas[i].active != this.areas[i].entered) {
  229.             changed = true;
  230.          }
  231.       }
  232.  
  233.       if (changed) {
  234.          ((Component)this).repaint();
  235.       }
  236.  
  237.       return true;
  238.    }
  239.  
  240.    public boolean mouseDown(Event evt, int x, int y) {
  241.       this.pressX = x;
  242.       this.pressY = y;
  243.  
  244.       for(int i = 0; i < this.areas.length; ++i) {
  245.          if (this.areas[i].inside(x, y)) {
  246.             this.areas[i].press(x, y);
  247.             if (this.areas[i].terminal) {
  248.                break;
  249.             }
  250.          }
  251.       }
  252.  
  253.       return true;
  254.    }
  255.  
  256.    public boolean mouseUp(Event evt, int x, int y) {
  257.       for(int i = 0; i < this.areas.length; ++i) {
  258.          if (this.areas[i].inside(this.pressX, this.pressY)) {
  259.             this.areas[i].lift(x, y);
  260.             if (this.areas[i].terminal) {
  261.                break;
  262.             }
  263.          }
  264.       }
  265.  
  266.       return true;
  267.    }
  268.  
  269.    public boolean mouseDrag(Event evt, int x, int y) {
  270.       this.mouseMove(evt, x, y);
  271.  
  272.       for(int i = 0; i < this.areas.length; ++i) {
  273.          if (this.areas[i].inside(this.pressX, this.pressY)) {
  274.             this.areas[i].drag(x, y);
  275.             if (this.areas[i].terminal) {
  276.                break;
  277.             }
  278.          }
  279.       }
  280.  
  281.       return true;
  282.    }
  283. }
  284.